<template>
  <div :class="[prefixCls, { fullscreen }]">
    <Upload
      name="file"
      multiple
      @change="handleChange"
      :action="uploadUrl"
      :customRequest="customRequest"
      :showUploadList="false"
      accept=".jpg,.jpeg,.png"
    >
      <a-button type="primary" v-bind="{ ...getButtonProps }">
        {{ t('component.upload.imgUpload') }}
      </a-button>
    </Upload>
  </div>
</template>
<script lang="ts">
  import { defineComponent, computed } from 'vue';

  import {message, Upload} from 'ant-design-vue';
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useI18n } from '/@/hooks/web/useI18n';
  import {uploadApi} from "/@/api/attachment/attach";
  import {UserService} from "/@/api/services";

  export default defineComponent({
    name: 'TinymceImageUpload',
    components: { Upload },
    props: {
      uploadUrl: {
        type: String,
        default: UserService + 'v1/attachment/upload'
      },
      fullscreen: {
        type: Boolean,
      },
      disabled: {
        type: Boolean,
        default: false,
      },
    },
    emits: ['uploading', 'done', 'error'],
    setup(props, { emit }) {
      let uploading = false;
      const { t } = useI18n();
      const { prefixCls } = useDesign('tinymce-img-upload');

      const getButtonProps = computed(() => {
        const { disabled } = props;
        return {
          disabled,
        };
      });

      function handleChange(info: Recordable) {
        const file = info.file;
        const status = file?.status;
        const url = file?.response?.url;
        const name = file?.name;

        if (status === 'uploading') {
          if (!uploading) {
            emit('uploading', name);
            uploading = true;
          }
        } else if (status === 'done') {
          emit('done', name, url);
          uploading = false;
        } else if (status === 'error') {
          emit('error');
          uploading = false;
        }
      }

      async function customRequest(formData) {
        formData.filename = formData.file.name;
        const result = await uploadApi(formData);
        const { data } = result;
        if (data && data.code === 0) {
          const {url} = data.result;
          emit('done', formData.file.name, url);
          message.success('上传成功');
        } else {
          message.error('上传失败');
        }
        uploading = false;
      }

      return {
        prefixCls,
        handleChange,
        customRequest,
        t,
        getButtonProps,
      };
    },
  });
</script>
<style lang="less" scoped>
  @prefix-cls: ~'@{namespace}-tinymce-img-upload';

  .@{prefix-cls} {
    position: absolute;
    top: 4px;
    right: 10px;
    z-index: 20;

    &.fullscreen {
      position: fixed;
      z-index: 10000;
    }
  }
</style>
